package com.comp5511.assignment01;


import java.io.*;

public class Stack {
	public Stack(Reader reader) {
		/*----------------------------------------------------------
		 * Constructor , initialize the parameters in this stack class
		- --------------------------------------------------------*/

		// Initialize a new Linklist when initializing the Stack_Method class
		this.currentHead = new Linklist();
		// Save the file reader which get the file Location in the main function
		this.reader = reader;
		// Initialize the rest stuff
		this.strContentReader = null;
		this.tempStr = null;
	}

	/*-----------------------------------------------------------------------------
	 * This function load the string, creates a new node ,and makes the new node
	 * to be the new head of the linklist
	 ------------------------------------------------------------------------------*/
	public void push(String tempStr) {
		this.currentHead = this.currentHead.insertNode(currentHead, tempStr);
	}

	/*-------------------------------------------------------------------------------
	 * This function just removes the current head from the linklist, and makes the 
	 * next node to be the new head of linklist
	 --------------------------------------------------------------------------------*/
	public void pop() {
		if (this.currentHead== null)
			System.out.println("No element in the stack");
		else
			this.currentHead = this.currentHead.getTopNode(currentHead);
	}

	/*-----------------------------------------------------
	 * Read file function. Use to get data from file.
	 ------------------------------------------------------*/

	public void fileReader() throws IOException {
		// Initialize the bufferReader ,load the file reader's instance
		this.strContentReader = new BufferedReader(this.reader);
		// Check whether the file is empty,if not, read string line by line
		while ((tempStr = strContentReader.readLine()) != null) {
			// delete the whitespaces which were read by readline function
			tempStr = tempStr.trim();
			// Condition matching function,according to every string read by
			// readline
			// function
			switch (tempStr) {
			// When PUSH(INSERT) was read, just continue, the next string will
			// match
			// the default
			case "PUSH(INSERT)":
				break;
			// When PRINT was read,display all node's data in screen by calling
			// specific
			// function
			case "PRINT":
				this.currentHead.showAllLinklist(currentHead);
				break;
			// When POP was read, remove the current head of the linklist, the
			// next node
			// will become a new head
			case "POP":
				this.currentHead = this.currentHead.getTopNode(currentHead);
				break;
			// This is used for getting strings, and inserting them to linklist
			default:
				this.currentHead = currentHead.insertNode(currentHead, tempStr);
				break;
			}
		}
	}

	// Show all node's data
	public void showLinkList() {
		this.currentHead.showAllLinklist(currentHead);
	}

	// Show current head node's data

	public void showCurrentHead() {
		System.out.println(this.currentHead.getData());
	}

	// Parameters, need to use the provided function to access.
	private String tempStr;
	private Reader reader;
	private BufferedReader strContentReader;
	private Linklist currentHead;
}
